<template>
  <div>
    <div class="departments-container">
      <div class="app-container">
        <!-- 卡片界面 -->
        <el-card class="box-card">
          <el-tabs v-model="activeName">
            <el-tab-pane label="组织架构" name="first" class="tab-pane">
              <!-- 粘性布局 -->
              <el-row type="flex" justify="space-between" align="middle" class="department-header">
                <el-col :span="20"><div class="grid-content bg-purple" />
                  <i class="el-icon-s-home" />
                  <span class="company">江苏传智播客教育科技股份有限公司</span>
                </el-col>
                <el-col :span="4">
                  <el-row type="flex">
                    <el-col>负责人</el-col>
                    <el-col>
                      <el-dropdown @click.native="op=!op">
                        <span class="el-dropdown-link">
                          操作<i class="el-icon-arrow-down el-icon--right" />
                        </span>
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="addDeptFn(''),isshow=true">添加子部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>

              </el-row>
              <!-- 树形控件 -->
              <el-tree
                :slot-scope="{ data }"
                class="departments-tree"
                :default-expand-all="true"

                :data="terrData"
                :props="defaultProps"
              >
                <!-- 主体区域绘制 -->

                <template slot-scope="{ data }">
                  <el-row type="flex" justify="space-between" style="height: 50px; width: 100%;" align="middle">
                    <el-col :span="20">
                      <span>{{ data.name }}</span>
                    </el-col>
                    <el-col :span="4">
                      <el-row type="flex" justify="end">
                        <el-col>{{ data.manager }}</el-col>
                        <el-col>
                          <el-dropdown>
                            <span class="el-dropdown-link">
                              操作<i class="el-icon-arrow-down el-icon--right" />
                            </span>
                            <el-dropdown-menu slot="dropdown">
                              <el-dropdown-item @click.native="addDeptFn(data.id),isshow=true">添加子部门</el-dropdown-item>
                              <el-dropdown-item @click.native="addDeptFn(data.id,true),isshow=false">编辑部门</el-dropdown-item>
                              <el-dropdown-item v-if="data && !data.children" @click.native="remove(data.id)">删除部门</el-dropdown-item>

                            </el-dropdown-menu>
                          </el-dropdown>
                        </el-col>
                      </el-row>
                    </el-col>
                  </el-row>
                </template>

              </el-tree>
              <el-dialog
                :title="isshow?'新增部门':'编辑部门' "
                :visible.sync="dialogVisible"
                width="50%"
                :close-on-click-modal="false"
                :close-on-press-escape="false"
                center
              >
                <Depatments v-if="dialogVisible" :department-list="orinigList" :pop-id="add" @remove="dialogVisible=false" @phpfalse="okkFn" />
                <span slot="footer" class="dialog-footer" />
              </el-dialog>
            </el-tab-pane>

          </el-tabs>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
import { DepartmentsAPI, removeDepartmentAPI } from '@/api/departmentsAPI'
import { TreeData } from '@/utils/shuxin.js'
import Depatments from './depatments.vue'

export default {
  name: 'Departments',
  components: {
    Depatments
  },
  data() {
    return {
      activeName: 'first',
      data: [
        {
          name: '一级 1',
          childrenList: [{
            name: '二级 1-1'

          }]
        }],
      defaultProps: {

        label: 'name'
      },
      terrData: [], //  数据列表
      dialogVisible: false, //  弹窗状态
      add: '', // 父传子
      isshow: true, // title提示
      op: false,
      orinigList: [] // 数据处理

    }
  },
  created() {
    this.DepartmentsAPI()
  },
  methods: {
    async  DepartmentsAPI() {
      const res = await DepartmentsAPI()
      const depts = res.data.depts
      this.terrData = TreeData(depts, '')
      // 数据处理
      this.orinigList = depts.map(itme => ({
        id: itme.id,
        code: itme.code,
        name: itme.name,
        pid: itme.pid
      }))
    },

    // 父传子
    addDeptFn(id, isEdit) {
      this.add = { id, isEdit }
      this.dialogVisible = true
    },

    okkFn() {
      // 字传父
      this.dialogVisible = false
      this.DepartmentsAPI()
    },

    // 删除部门
    async remove(id) {
      const confimRes = await this.$confirm('确定删除吗', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(e => e)

      confimRes === 'cancel'
        ? this.$message('取消了')
        : await removeDepartmentAPI(id); this.$message.success('删除成功'); this.DepartmentsAPI()

      // let i = 0

      // if (this.op) {
      //   setInterval(async() => {
      //     i++
      //     this.terrData[i].id
      //     const res = await removeDepartmentAPI(this.terrData[i].id)
      //     console.log(res)
      //     this.DepartmentsAPI()
      //   }, 1000)
      // }
    }
  }

}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
.departments-tree {
  margin: 12px 0px;
}
</style>
